/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.view;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.itsc.ds2.bl.MusicalGenreBlLocal;
import mx.itsc.ds2.model.MusicalGenres;

/**
 *
 * @author Eli
 */
@ManagedBean
@SessionScoped
public class MusicalGenresBean {
    @EJB
    private MusicalGenreBlLocal musicalGenreBl;

    /**
     * Creates a new instance of MusicalGenresBean
     */
    private List<MusicalGenres> musicalGenresList;
    private MusicalGenres musicalGenres;
    private String action;
    private int selectedMusicalGenre;
    
    public MusicalGenresBean() {
        musicalGenres = new MusicalGenres();
    }

    public int getSelectedMusicalGenre() {
        return selectedMusicalGenre;
    }

    public void setSelectedMusicalGenre(int selectedMusicalGenre) {
        this.selectedMusicalGenre = selectedMusicalGenre;
    }

    public MusicalGenres getMusicalGenres() {
        return musicalGenres;
    }

    public void setMusicalGenres(MusicalGenres musicalGenres) {
        this.musicalGenres = musicalGenres;
    }

    public List<MusicalGenres> getMusicalGenresList() {
        musicalGenresList=musicalGenreBl.getAll();
        return musicalGenresList;
    }
    //----------------------------------------------------------------------------
    
     public String procesarAccion() {
        if (action.equalsIgnoreCase("Nuevo")) {
            musicalGenreBl.register(musicalGenres);
        } else {
            if (action.equalsIgnoreCase("Editar")) {
                musicalGenreBl.modify(musicalGenres);
            } else {
                if (action.equalsIgnoreCase("Eliminar")) {
                    musicalGenreBl.delete(musicalGenres);
                }
            }        }


        action = "";
        return "MusicalGenresList";
    }
     
    
      public String prepararCancelar(){
        String regresa = "";
        if (action.equalsIgnoreCase("Eliminar")) {
           regresa = "MusicalGenresList";
           
        } if(action.equalsIgnoreCase("Editar")|| action.equalsIgnoreCase("Nuevo")){
            regresa = "CreateEditMusicalGenres";
        }
        return regresa;
    }

      //--------------------------------------------------------------------------------
      
    public void prepararNuevo(ActionEvent e) {//javax.faces.event.ActionEventjhgew
        action = "Nuevo";
        musicalGenres = new MusicalGenres();
    }

    public void prepararEditar(ActionEvent e) {
        action = "Editar";
        int index = Integer.parseInt(e.getComponent().getClientId().split(":")[2]);
        musicalGenres = musicalGenresList.get(index);
    }

    public void prepararEliminar(ActionEvent e) {
        action = "Eliminar";
        int index = Integer.parseInt(e.getComponent().getClientId().split(":")[2]);
        musicalGenres = musicalGenresList.get(index);
    }

    
}